package com.bluefish.bltuan.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bluefish.bltuan.dao.OrderDao;
import com.bluefish.bltuan.module.shop.Order;
import com.bluefish.bltuan.module.shop.Ticket;
import com.bluefish.bltuan.module.shop.TuanGou;
import com.bluefish.bltuan.module.user.User;
import com.bluefish.bltuan.service.OrderService;
import com.bluefish.bltuan.service.TicketService;
import com.bluefish.bltuan.util.PageIndex;

@Service
@Transactional
public class OrderServiceImpl implements OrderService{

	private OrderDao orderDao;
	private TicketService ticketService;
	
	public void setTicketService(TicketService ticketService) {
		this.ticketService = ticketService;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void save(Order t) {
		if(doZhiFu())
			//完成支付
			t.pay();
		else
			t.unPay();
		this.orderDao.save(t);
		saveTickets(t);
	}

	private void saveTickets(Order order){
		for(int i=0;i<order.getPiece();i++){
			Ticket ticket = new Ticket();
			ticket.initByOrder(order);
			this.ticketService.save(ticket);
		}
	}
	
	/**
	 * 调用支付宝接口
	 * @return
	 */
	private boolean doZhiFu(){
		//TODO
		return true;
	}
	
	public void update(Order t) {
		// TODO Auto-generated method stub
		
	}

	public void delete(Order t) {
		// TODO Auto-generated method stub
		
	}

	public Order findById(Integer id) {
		return this.orderDao.findById(id);
	}

	public List<Order> getOrdersByUser(User user, PageIndex pageIndex) {
		return this.orderDao.listByUser(user, pageIndex);
	}

	public List<Order> getOrdersByTuangou(TuanGou tuanGou, PageIndex pageIndex) {
		return this.orderDao.listByTuangou(tuanGou, pageIndex);
	}

	
}
